﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Windows.Input;
using System.Threading;
using System.Reflection;
using System.IO;
using System.Windows.Interop;


namespace MigratorUI.RenderEngine
{
   public class Sprite : UIElement
    {

       public Sprite(String id, System.Drawing.Bitmap bitmap, bool isDragable,int radius)
       {
           
            this.IsDragable = isDragable;
            this.Id = id;
            this.Radius = radius;
            this.LastClickedPointInDrawableItem = new Point(0, 0);
            this.Image = new Image();
            LoadImage(bitmap);
            CreateContextMenu();
            this.Image.MouseLeftButtonDown += new MouseButtonEventHandler(ImageOnMouseDown);
          
           this.RepeatAnimationsList = new List<Animation>();
           
        }

        private void LoadImage(System.Drawing.Bitmap bitmap)
        {
            BitmapSource bs = Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
            this.Image.Source = bs;
            this.Image.Width = bs.Width;
            this.Image.Height = bs.Height;

        }

        private void CreateContextMenu()
        {
            this.contextMenu = new ContextMenu();
            this.removeMenuItem = new MenuItem();
            this.viewPacketsReceivedMenuItem = new MenuItem();
            this.viewPacketsSentMenuItem = new MenuItem();
            this.sendPacketMenuItem = new MenuItem();
            this.removeMenuItem.Header = "Remove";
            this.removeMenuItem.Click += new RoutedEventHandler(RemoveMenuItemClick);
            this.viewPacketsReceivedMenuItem.Header = "View Packets received";
            this.viewPacketsReceivedMenuItem.Click += new RoutedEventHandler(ViewPacketsReceivedMenuItemClick);
            this.viewPacketsSentMenuItem.Header = "View Packets sent";
            this.viewPacketsSentMenuItem.Click += new RoutedEventHandler(ViewPacketsSentMenuItemClick);
            this.sendPacketMenuItem.Header = "Send Packet";
            this.sendPacketMenuItem.Click += new RoutedEventHandler(SendPacketsMenuItemClick);
            this.contextMenu.Items.Add(this.viewPacketsReceivedMenuItem);
            this.contextMenu.Items.Add(this.viewPacketsSentMenuItem);
            this.contextMenu.Items.Add(this.sendPacketMenuItem);
            this.contextMenu.Items.Add(this.removeMenuItem);
            this.Image.ContextMenu = this.contextMenu;    
        }

        public UIElement GetDrawableItem() {
            return this.Image;
        }       
        


        private void ImageOnMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (IsDragable)
            {
               
                if (this.SpritedSelectedEvent != null) this.SpritedSelectedEvent.Invoke(this);
                if (this.SpriteMovedEvent != null) this.SpriteMovedEvent.Invoke(this);

                this.LastClickedPointInDrawableItem = e.GetPosition(this.Image);
                System.Windows.DataObject sprite = new System.Windows.DataObject(Sprite.spriteFormat.Name, this);
                System.Windows.DragDrop.DoDragDrop(this, sprite, System.Windows.DragDropEffects.Copy);
            }
        }

        private void RemoveMenuItemClick(object sender, RoutedEventArgs e)
        {
            if (SpriteRemovedEvent != null) SpriteRemovedEvent.Invoke(this);
        }

        private void ViewPacketsReceivedMenuItemClick(object sender, RoutedEventArgs e)
        {
            if (ViewPacketsReceivedEvent != null) ViewPacketsReceivedEvent.Invoke(this);
        }

        private void ViewPacketsSentMenuItemClick(object sender, RoutedEventArgs e)
        {
            if (ViewPacketsSentEvent != null) ViewPacketsSentEvent.Invoke(this);
        }

        private void SendPacketsMenuItemClick(object sender, RoutedEventArgs e)
        {
            if (SendPacketsEvent != null) SendPacketsEvent.Invoke(this);
        }
        

        public String Id {

            get { return this.id; }
            set {
                String oldId = this.id;
                this.id = value;
                if (this.IdChangedEvent != null) this.IdChangedEvent.Invoke(this, oldId);
            
            } 
        }
       
        //private
        private String id;
        private ContextMenu contextMenu;
        private MenuItem removeMenuItem;
        private MenuItem viewPacketsReceivedMenuItem;
        private MenuItem viewPacketsSentMenuItem;
        private MenuItem sendPacketMenuItem;
        
        //public
        public Point LastClickedPointInDrawableItem{get;set;}
        public List<Animation> RepeatAnimationsList { get; set; } // animations that repeat
        public Image Image { get; set; }
        public Point Position { get; set; }
        public float Radius { get; set; }
        public bool IsDragable { get; set; }
        
        //events 
        public delegate void SpriteRemovedEventHandler(Sprite sprite);
        public event SpriteRemovedEventHandler SpriteRemovedEvent;
        public delegate void SpriteMovedEventHandler(Sprite sprite);
        public event SpriteMovedEventHandler SpriteMovedEvent;
        public delegate void IdChangedEventHandler(Sprite sprite,String oldId);
        public event IdChangedEventHandler IdChangedEvent;
        public delegate void SpritedSelectedEventHandler(Sprite sprite);
        public event SpritedSelectedEventHandler SpritedSelectedEvent;
        
        public delegate void ViewPacketsEventHandler(Sprite sprite);
        public event ViewPacketsEventHandler ViewPacketsReceivedEvent;
        public event ViewPacketsEventHandler ViewPacketsSentEvent;


        public delegate void SendPacketsEventHandler(Sprite sprite);
        public event SendPacketsEventHandler SendPacketsEvent;

        //static
        public static System.Windows.DataFormat spriteFormat = DataFormats.GetDataFormat("SpriteFormat");

    }
}
